import java.awt.Polygon;
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.UnsupportedAudioFileException;



public class Bullet implements GameObject {
	private int posX;
	private int posY;
	private int angle;
	private boolean life;
	private int longlife;
	private float speed;
	private Polygon shape;
	
	public Bullet(SpaceShip s,int speed) {
		// TODO Auto-generated constructor stub
		posX=s.getPosX();
		posY=s.getPosY();
		this.speed=speed;
		angle =s.getAngle();
		longlife = SpaceWar.ScreenHeight;
		life=true;
		
		Runnable r2 =new Runnable() {
			public void run() {

		        try {
		                AudioInputStream audio = AudioSystem.getAudioInputStream(new File("sfx_crossbow_fire.wav"));
		                Clip clip = AudioSystem.getClip();
		                clip.open(audio);
		                clip.start();
		        }
		        
		        catch(UnsupportedAudioFileException uae) {
		                System.out.println(uae);
		        }
		        catch(IOException ioe) {
		                System.out.println(ioe);
		        }
		        catch(LineUnavailableException lua) {
		                System.out.println(lua);
		        }
			}
		};
		Thread thr2 = new Thread(r2);
		thr2.start();
	}
	
	public void setLongLife(int l){
		longlife = l;
	}
	@Override
	public int getPosX() {
		// TODO Auto-generated method stub
		return posX;
	}

	@Override
	public void setPosX(int posX) {
		// TODO Auto-generated method stub
		this.posX=posX;
	}

	@Override
	public int getPosY() {
		// TODO Auto-generated method stub
		return posY;
	}

	@Override
	public void setPosY(int posY) {
		// TODO Auto-generated method stub
		this.posY=posY;
	}

	@Override
	public float getSpeed() {
		// TODO Auto-generated method stub
		return speed;
	}

	@Override
	public void setSpeed(float speed) {
		// TODO Auto-generated method stub
		this.speed=speed;
	}

	@Override
	public int getAngle() {
		// TODO Auto-generated method stub
		return angle;
	}

	@Override
	public void setAngle(int angle) {
		// TODO Auto-generated method stub
		this.angle=angle;
	}

	@Override
	public void setLife(boolean life) {
		// TODO Auto-generated method stub
		this.life=life;
	}

	@Override
	public boolean getLife() {
		// TODO Auto-generated method stub
		return life;
	}

	@Override
	public void setShape(Polygon shape) {
		// TODO Auto-generated method stub
		this.shape=shape;
	}

	@Override
	public Polygon getShape() {
		// TODO Auto-generated method stub
		return shape;
	}

	@Override
	public void createShape() {
		// TODO Auto-generated method stub
		shape = new Polygon();
		shape.addPoint((int) posX-2, (int) posY-2);
		shape.addPoint((int) posX+2, (int) posY-2);
		shape.addPoint((int) posX+2, (int) posY+2);
		shape.addPoint((int) posX-2, (int) posY+2);

	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		//System.out.println("Shoting...");
		float radians = (float) Math.toRadians(angle);
		int delx= (int) ((speed)*Math.cos(radians) - (0)* Math.sin(radians)); 
		int dely = (int) ((speed)*Math.sin(radians) + (0)*Math.cos(radians));
		posX +=Math.ceil(delx);
		posY +=Math.ceil(dely);
		
		if (posX > SpaceWar.ScreenWidth)
			posX = 0;
		if (posX < 0)
			posX = SpaceWar.ScreenWidth;

		if (posY > SpaceWar.ScreenHeight)
			posY = 0;
		if (posY < 0)
			posY = SpaceWar.ScreenHeight;
		longlife-=speed;
		if(longlife<0){
			life=false;
		}
		createShape();
	}
	public void calculateTrajectory(int angle)
	{
		
		
	}

}
